import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:flutter_yd_simple/Project/ScanModule/PublicModule/ScanViewModule.dart';
import 'package:fluttertoast/fluttertoast.dart';

class DisScanPageDemo extends StatefulWidget {
  const DisScanPageDemo({super.key});

  @override
  State<DisScanPageDemo> createState() => _DisScanPageDemoState();
}

class _DisScanPageDemoState extends State<DisScanPageDemo> {

  var ItemAry = [{"title":"单个扫描","singleNormalIcon":"lib/Project/Images/Scan/project_singleScan_normal.png",
                  "batchNormalIcon":"lib/Project/Images/Scan/lucency_singleScan_normal.png",
                  "selectIcon":"lib/Project/Images/Scan/project_singleScan_select.png"},

                {"title":"批量扫描","singleNormalIcon":"lib/Project/Images/Scan/project_batchScan_normal.png",
                  "batchNormalIcon":"lib/Project/Images/Scan/lucency_batchScan_select.png",
                  "selectIcon":"lib/Project/Images/Scan/lucency_batchScan_select.png"},

                {"title":"手动输入","singleNormalIcon":"lib/Project/Images/Scan/project_handImport_normal.png",
                  "batchNormalIcon":"lib/Project/Images/Scan/lucency_handImport_normal.png",
                  "selectIcon":"lib/Project/Images/Scan/project_handImport_select.png"},

                {"title":"语音输入","singleNormalIcon":"lib/Project/Images/Scan/project_voiceImport_normal.png",
                "batchNormalIcon":"lib/Project/Images/Scan/lucency_voiceImport_normal.png",
                "selectIcon":"lib/Project/Images/Scan/project_voiceImport_select.png"}];
  
  var selectIndexPage = 0;
  var mailNoList = [];
  var mailNoProcessing = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF6F8FA),
      appBar: AppBar(
        title: Text("分发扫描"),
      ),
      body:Column(
        children: [
          Container(
            height: 40,
            color: Color(0xFFECF4FF),
            padding: EdgeInsets.only(top: 8.5,bottom: 8.5,left: 16,right: 16),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                OutlinedButton(
                  onPressed: (){

                  } , 
                  child: Text('开启极速扫描',style: TextStyle(color: Color(0xFF1678FF),fontSize: 12)),
                  style: OutlinedButton.styleFrom(
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(16)
                    ),
                    side: BorderSide(width: 1,color: Color(0xFF1678FF))
                  ),
                ),
                OutlinedButton(
                  onPressed: (){

                  }, 
                  child: Text('连接蓝牙设备',style: TextStyle(color: Color(0xFF1678FF),fontSize: 12)),
                  style: OutlinedButton.styleFrom(
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(16)
                    ),
                    side: BorderSide(width: 1,color: Color(0xFF1678FF))
                  ),
                )
            
              ],
            ),
          ),

          Container(
            padding: EdgeInsets.only(top: 8.5,bottom: 8.5,left: 16,right: 16),
            height: 36,
            child: Row(
              children: [
                Text("派件员：8037-张Sir(18311112222)",style: TextStyle(color: Color(0xFF222222),fontSize: 16))
              ],
            ),
          ),

          Container(
            height: 200,
            color: Colors.blue,
            child:ScanViewModuleDemo(
              blockStr: (value) {
                if (false == mailNoProcessing) {
                  mailNoProcessing = true;
                  setState(() {
                    mailNoList.add(value);
                  });
                  MusicPlay('lib/Project/VoiceResources/Scan.wav');
                  /*
                  final player = AudioPlayer();
                  player.play(
                    AssetSource('lib/Project/VoiceResources/Scan.wav')
                  );
                  */
                  print(value);
                  Future.delayed(Duration(seconds: 2)).then((value) => {
                    mailNoProcessing = false,
                    print("123"),
                  });
                }
              },
            ),
          ),

          Container(
            height: 68,
            child: Row(
               mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                for(int i = 0 ; i < ItemAry.length; i++ )
                GestureDetector(
                  onTap: () {
                    setState(() {
                      selectIndexPage = i;
                    });
                    Fluttertoast.showToast(msg: "点击了"+ (ItemAry[i]["title"] as String),gravity: ToastGravity.CENTER);
                  },
                  child: Column(
                    children: [
                      SizedBox(height: 10),
                      Image.asset(
                        selectIndexPage == i?ItemAry[i]["selectIcon"]!:ItemAry[i]["singleNormalIcon"]!,
                        width: 68,height: 48
                      ),
                    ]
                  )                  
                )
        
                ],
            ),
          ),

          Expanded(child:Container(
            child: ListView.builder(
              itemCount: mailNoList.length,
              itemBuilder: (context, index) {
                return Container(
                  color: Colors.white,
                  margin: EdgeInsets.only(top: 8),
                  child: Row(children: [
                    IconButton(
                      onPressed: () {
                      setState(() {
                        //itemAry[index]["select"] = itemAry[index]["select"]==1?0:1;
                      });
                      },
                      //icon: Image.asset(itemAry[index]["select"]==1?'lib/Project/Images/Sign/sign_icon_select.png':'lib/Project/Images/Sign/sign_icon_normal.png',width: 23,height: 23)),
                    icon: Image.asset('lib/Project/Images/Sign/sign_icon_normal.png',width: 23,height: 23)),

                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        SizedBox(height: 12),
                        SizedBox(height:24,
                          child:Text(mailNoList[index],style: TextStyle(fontSize: 14,color: Color(0xFF222222),fontWeight: FontWeight.w500)),
                        ),
                        SizedBox(height: 6),
                      ],
                    )
                  ],),
                );
            
              }
            )
          ),),

          Container(
            height: 90,
            color: Colors.white,
            child: Row(
              children: [
                SizedBox(width: 10),
                GestureDetector(
                  onTap: () {
                    
                  }, 
                  child: Image.asset('lib/Project/Images/Sign/sign_icon_normal.png',width: 25,height: 25)
                ),
                SizedBox(width: 7),
                Text('全选(已选0)',style: TextStyle(fontSize: 16,color: Color(0xFF222222))),
                SizedBox(width: 8),

                Expanded(
                  child: OutlinedButton(
                    onPressed: (){

                    } , 
                    child: Text('   发短信   ',style: TextStyle(color: Color(0xFF1678FF),fontSize: 12)),
                    style: OutlinedButton.styleFrom(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(4)
                      ),
                      side: BorderSide(width: 1,color: Color(0xFF1678FF))
                    ),
                  ),
                ),
                SizedBox(width: 8),

                Expanded(
                  child: OutlinedButton(
                    onPressed: (){

                    }, 
                    child: Text('  立即上传  ',style: TextStyle(color: Colors.white,fontSize: 12)),
                    style: ButtonStyle(
                      backgroundColor: MaterialStateProperty.all<Color>(Color(0xFF1678FF)), // 设置背景颜色
                    ),
                  ),
                ),
                SizedBox(width: 15),

              ],
            ),
          )
          //)
        ],
      ),
    );
  }


  Future MusicPlay(String path)async {
    final player = AudioPlayer();
    await player.setSource(DeviceFileSource(path,mimeType: "wav"));
    //await player.resume();
  }
}